import fs from 'node:fs'
import Path from 'node:path'

const template
= `<template>
  <m-app :theme-vars="{ }">
    <view>demo</view>
  </m-app>
</template>

<script setup>
import { onHide, onLoad, onShow, onUnload } from '@dcloudio/uni-app'
import { ref } from 'vue'

const test = ref('demo')
</script>

<style lang="scss" scoped>

</style>
`
export function createFile(p: string, tmp = '') {
  // 根据path 创建文件夹,如果中间任意一级路径不存在，则依次创建符合路径的文件夹层级
  const dirPath = Path.dirname(p)
  try {
    // 检查目录是否存在，如果不存在则创建
    if (!fs.existsSync(dirPath)) {
      console.log('目录不存在，正在创建...')
      fs.mkdirSync(dirPath, { recursive: true })
    }

    if (!fs.existsSync(p)) {
      fs.writeFileSync(p, tmp || template, { flag: 'w' })
    }
  }
  catch (error) {
    console.log(error)
  }
}
